#include "lcd1602.h"
#include "blueteeth_srial.h"
#include "intrins.h"
#include "delay.h"
unsigned char R_string[4];
unsigned char G_string[4];
unsigned char B_string[4];
void LcdInit()
{
		Lcdwcmd(0x38);//16*2��ʾ��5*7����8λ����
		Lcdwcmd(0x0c);//��ʾ�����ع��
		Lcdwcmd(0x06);//дһ���ַ�ָ��󣬵�ַָ���һ
		Lcdwcmd(0x01);//���LCD����ʾ����
}

void Lcdposition_set(unsigned char pos)
{
		Lcdwcmd(pos|0x80);//����ָ�� = 80 + ��ַ����
}

void Lcdwcmd(unsigned char cmd)
{
		RS = 0;
		RW = 0;
		EN = 0;
		_nop_();
		_nop_();
		LCD_data = cmd;
//		_nop_();_nop_();_nop_();
		delay10us(1);
		EN = 1;
//		_nop_();_nop_();_nop_();
		delay10us(1);
		EN = 0;
}

void Lcdwdata(unsigned char dat)
{
		RS = 1;
		RW = 0;
		EN = 0;
		_nop_();
		_nop_();
		LCD_data = dat;
//		_nop_();_nop_();_nop_();
	  delay10us(1);
		EN = 1;
//		_nop_();_nop_();_nop_();
		delay10us(1);
		EN = 0;
}

void send_string(unsigned char*s)
{
		while(*s!='\0')
		{
			Lcdwdata(*s);
			s++;
		}
}

void IntToString(unsigned char *str, int dat)
{
    signed char i = 0;
    unsigned char	buf[6];
    do {                           //��ת��Ϊ��λ��ǰ��ʮ��������
        buf[i ++] = dat % 10;
        dat /= 10;
			}
		while (dat > 0);
    while (i-- > 0)                //������ֵת��ΪASCII�뷴�򿽱�������ָ����
    {
        *str++ = buf[i] + '0';
    }
    *str = '\0';                   //�����ַ���������   
}

void LCD_display()
{
		IntToString(R_string, R_value);//����ת�ַ�������
		Lcdposition_set(0x41);//��λ�ڶ��еڶ���
		send_string(R_string);//�����ַ���
		IntToString(G_string, G_value);
		Lcdposition_set(0x46);
		send_string(G_string);
		IntToString(B_string, B_value);
		Lcdposition_set(0x4b);
		send_string(B_string);
}